package org.jugile.proto2.load;

import org.jugile.proto2.domain.*;
import org.jugile.util.Jugile;

public class WriteNode extends Jugile {

	private static String node;
	public static void main(String args[]) throws Exception {
		node = args[0];
		if (empty(node)) node = "admin1";
		Domain.setNode(node);
		Domain d = Domain.getDomain();
		for (int i = 0; i < 100; i++) {
			int deltas = d.update();
			log("writer update deltas: " + deltas);
			try {
				doit(d,node,i);
				d.commit();
			} catch (Exception e) {
				d.rollback();
				log("error: " + e);
			}
			Thread.sleep((int)(Math.random()*500));
		}
		// cool down
		for (int i = 0; i < 10; i++) {
			d.update();
			int size = d.getPersons().size();
			d.rollback();
			log(""+i+". persons: " + size);
			Thread.sleep(10000);
		}
	}
	
	private static void doit(Domain d, String node, int i) throws Exception {
		for (int j = 0; j < 10; j++) {
			Person p = d.createPerson();
			p.setName("p"+j+"/"+i);
			p.setTs(now());
		}
		//for (Person p : d.getPersons().first(5,"id")) p.delete();	
	}
	
	public static void log(String msg) {
		print(node + ": " + msg);
	}
	
}
